package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//方法一:哈希表
//方法二:双指针迭代:24ms 96%, 7MB 82%
//两个节点nil == nil是true,单独nil比较不可以
func getIntersectionNode(headA, headB *ListNode) *ListNode {
	i, j := headA, headB
	for i != j {
		if i == nil {
			i = headB
		} else {
			i = i.Next
		}
		if j == nil {
			j = headA
		} else {
			j = j.Next
		}
	}
	return i
}

func main() {
	a, b := ListNode{1, nil}, ListNode{2, nil}
	fmt.Println(getIntersectionNode(&a, &b))
}
